package basic.lessons;

import java.awt.*;
import java.applet.Applet;

public class StaticRectangularApp extends Applet {
   
private int rectHeight;
private int rectWidth;
private int numberOfRects;

{TextIO.putln("Please, enter Rectangular Width in pixels and press \"Enter\""); 
this.rectWidth = TextIO.getInt(); // reading Rectangular width from console

TextIO.putln("Please, enter Rectangular Height in pixels and press \"Enter\"");
this.rectHeight = TextIO.getInt(); // reading Rectangular height from console

TextIO.putln("Please, enter desired number of Rectangulars and press \"Enter\"");
this.numberOfRects = TextIO.getlnInt(); // reading Rectangular height from console
}

public void paint(Graphics g) {

         // Draw a set of nested black rectangles on a red background.
         // Each nested rectangle is separated by 15 pixels on
         // all sides from the rectangle that encloses it.
         
      int insetW=0;    // Gap between borders of applet 
      int insetH=0;    //        and one of the rectangles.
                    
      int  rectWidth = this.rectWidth;
      int rectHeight = this.rectHeight;   // The size of the largest rectangle.
      int numberOfRects = this.numberOfRects;
      int n = this.numberOfRects; // variable to calculate next rectangular dimensions
      
            
      g.setColor(Color.red);
      g.fillRect(0,0,(int)rectWidth,(int)rectHeight);  // Fill the entire applet with red.
      g.setColor(Color.black);  // Draw the rectangles in black.
      
      
      
       
      while (n>=1 && rectWidth>0 && rectHeight >0) {    	  
    	 int iW =  Math.round((this.rectWidth+1)/numberOfRects);
    	 int iH =  Math.round((this.rectHeight+1)/numberOfRects);
    	 
         g.drawRect(insetW, insetH, rectWidth, rectHeight);
         insetW += Math.round((this.rectWidth+1)/2/numberOfRects);       // Rects are 15 pixels apart.
         insetH += Math.round((this.rectHeight+1)/2/numberOfRects); 
         rectWidth -=  iW; // Width decreases by 15 pixels //Math.round((rectWidth+1)*((n-1)/(float)numberOfRects)); 
                            //             on left and 15 on right.
         rectHeight -= iH;// Height decreases by 15 pixels //Math.round((rectHeight+1)*((n-1)/(float)numberOfRects));   
                            //             on top and 15 on bottom.
         n--;
      }
      
   }  // end paint()

}  // end class StaticRects